package com.example.onlinemonitoring.Util;

import com.example.onlinemonitoring.Util.tem.SensorAbstractBaseService;
import com.example.onlinemonitoring.model.common.pagebase;

import org.springframework.beans.BeanUtils;
import tk.mybatis.mapper.entity.Example;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.text.SimpleDateFormat;

public class ConditionDispose {

    /**
     * 默认的分组查询更新删除条件（带inuse、alarm、communication判断条件）
     *
     * @param criteria
     * @param record
     */
    public static void defaultDispose(Example.Criteria criteria, DefaultProperty record, Class t) {
        if (record.getStationid() != null) {
            criteria.andEqualTo("stationid", record.getStationid());
        }
        if (record.getDevicegroup() != null) {
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
        }
        if (record.getDeviceaddr() != null) {
            criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
        }
        if (record.getChunk() != null) {
            criteria.andEqualTo("chunk", record.getChunk());
        }
        if (record.getPointaddr() != null) {
            if (getField("pointaddr", t)) {
                criteria.andEqualTo("pointaddr", record.getPointaddr());
            }
        }
        if (record.getInuse() != null) {
            if (getField("inuse", t)) {
                criteria.andEqualTo("inuse", record.getInuse());
            }
        }
        if (record.getLastalarmlevel() != null) {
            if (getField("lastalarmlevel", t)) {
                criteria.andEqualTo("lastalarmlevel", record.getLastalarmlevel());
            }
        }
        if (record.getLastcommunicationstatus() != null) {
            criteria.andEqualTo("lastcommunicationstatus", record.getLastcommunicationstatus());
        }

    }

    /**
     * 默认的分组查询更新删除条件（带inuse、alarm、communication判断条件,带组区间）
     *
     * @param criteria
     * @param record
     */
    public static void defaultDispose(Example.Criteria criteria, DefaultProperty record, Class t, Integer groupstart,
            Integer groupend) {
        if (record.getStationid() != null) {
            criteria.andEqualTo("stationid", record.getStationid());
        }
        if (record.getDevicegroup() != null) {
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
        }
        if (record.getDeviceaddr() != null) {
            criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
        }
        if (record.getChunk() != null) {
            criteria.andEqualTo("chunk", record.getChunk());
        }
        if (record.getPointaddr() != null) {
            if (getField("pointaddr", t)) {
                criteria.andEqualTo("pointaddr", record.getPointaddr());
            }
        }
        if (record.getInuse() != null) {
            if (getField("inuse", t)) {
                criteria.andEqualTo("inuse", record.getInuse());
            }
        }
        if (record.getLastalarmlevel() != null) {
            if (getField("lastalarmlevel", t)) {
                criteria.andEqualTo("lastalarmlevel", record.getLastalarmlevel());
            }
        }
        if (record.getLastcommunicationstatus() != null) {
            criteria.andEqualTo("lastcommunicationstatus", record.getLastcommunicationstatus());
        }
        if (groupstart != null && groupend != null) {
            criteria.andBetween("devicegroup", groupstart, groupend);
        }
        if (record.getCmd() != null) {
            criteria.andEqualTo("cmd", record.getCmd());
        }
        if (record.getGas() != null) {
            criteria.andEqualTo("gas", record.getGas());
        }
        if (record.getLoginname() != null) {
            criteria.andEqualTo("loginname", record.getLoginname());
        }
    }

    /**
     * 默认用于更新inuse的拼接条件(条件不带inuse)
     *
     * @param criteria
     * @param record
     */
    public static void defaultDisposeByUpdateInuse(Example.Criteria criteria, DefaultProperty record) {
        if (record.getStationid() != null) {
            criteria.andEqualTo("stationid", record.getStationid());
        }
        if (record.getDevicegroup() != null) {
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
        }
        if (record.getDeviceaddr() != null) {
            criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
        }
        if (record.getPointaddr() != null) {
            criteria.andEqualTo("pointaddr", record.getPointaddr());
        }
    }

    /**
     * 默认用于查询的拼接条件(条件带inuse)
     *
     * @param criteria
     * @param record
     */
    public static void defaultDisposeByUpdate(Example.Criteria criteria, DefaultProperty record) {
        if (record.getStationid() != null) {
            criteria.andEqualTo("stationid", record.getStationid());
        }
        if (record.getDevicegroup() != null) {
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
        }
        if (record.getDeviceaddr() != null) {
            criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
        }
        if (record.getPointaddr() != null) {
            criteria.andEqualTo("pointaddr", record.getPointaddr());
        }
        if (record.getInuse() != null) {
            criteria.andEqualTo("inuse", record.getInuse());
        }
        if (record.getCmd() != null) {
            criteria.andEqualTo("cmd", record.getCmd());
        }
        if (record.getGas() != null) {
            criteria.andEqualTo("gas", record.getGas());
        }
        if (record.getLoginname() != null) {
            criteria.andEqualTo("loginname", record.getLoginname());
        }
    }

    /**
     * 查询报警和预警的记录条件
     *
     * @param criteria
     * @param record
     */
    public static void alarmDispose(Example.Criteria criteria, DefaultProperty record) {
        if (record.getStationid() != null) {
            criteria.andEqualTo("stationid", record.getStationid());
        }
        if (record.getDevicegroup() != null) {
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
        }
        if (record.getDeviceaddr() != null) {
            criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
        }
        if (record.getPointaddr() != null) {
            criteria.andEqualTo("pointaddr", record.getPointaddr());
        }
        if (record.getInuse() != null) {
            criteria.andEqualTo("inuse", record.getInuse());
        }
        if (record.getLastalarmlevel() != null) {
            criteria.andNotEqualTo("lastalarmlevel", record.getLastalarmlevel());
        }
    }

    /**
     * 查询报警和预警的记录
     *
     * @param record
     * @param t
     * @return
     */
    public static Example alarmDispose(DefaultProperty record, Class t) {
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        defaultDispose(criteria, record, t);
        return example;
    }

    /**
     * 获取查询条件对象,并封装
     *
     * @param record
     * @param t
     * @return
     */
    public static Example defaultDispose(DefaultProperty record, Class t) {
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        defaultDispose(criteria, record, t);
        return example;
    }

    /**
     * 获取查询条件对象,并封装
     *
     * @param record
     * @param t
     * @return
     */
    public static Example defaultDispose(DefaultProperty record, Class t, String[] properties) {
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        example.selectProperties(properties);
        defaultDispose(criteria, record, t);
        return example;
    }

    /**
     * 获取查询条件对象,并封装组
     *
     * @param record
     * @param t
     * @return
     */
    public static Example defaultDispose(Object obj, Class t, Integer groupstart, Integer groupend) {
        DefaultProperty record = copy(obj);
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        // example.orderBy("id").asc();
        example.setOrderByClause("devicegroup , deviceaddr asc");
        defaultDispose(criteria, record, t, groupstart, groupend);
        return example;
    }

    /**
     * 获取封装更新条件
     *
     * @param obj
     * @param t
     * @return
     */
    public static Example defaultDisposeByUpdate(Object obj, Class t) {
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        defaultDisposeByUpdate(criteria, copy(obj));
        return example;
    }

    /**
     * 获取查询条件对象,将obj封装成可以查询的对象
     *
     * @param obj
     * @param t
     * @return
     */
    public static Example defaultDispose(Object obj, Class t) {
        DefaultProperty record = copy(obj);
        return defaultDispose(record, t);
    }

    /**
     * 获取查询条件对象,将obj封装成可以查询的对象,有排除项
     *
     * @param obj
     * @param t
     * @return
     */
    public static Example defaultDispose(Object obj, Class t, String[] properties) {
        DefaultProperty record = copy(obj);
        return defaultDispose(record, t, properties);
    }

    /**
     * 封装查询第一条
     *
     * @param obj
     * @param t
     * @return
     */
    public static Example defaultDisposeByInUse(Object obj, Class t) {
        DefaultProperty record = copy(obj);
        if (getField("inuse", t)) {
            record.setInuse(1);
        } else {
            record.setInuse(null);
        }
        return defaultDisposeByUpdate(record, t);
    }

    /**
     * 判断对象是否拥有字段属性
     *
     * @param field 字段
     * @param t     对象
     * @return
     */
    private static boolean getField(String field, Class t) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(t);
            PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor descriptor : descriptors) {
                if (descriptor.getName().equals(field)) {
                    return true;
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 转换对象
     *
     * @param obj
     * @return
     */
    public static DefaultProperty copy(Object obj) {
        DefaultProperty defaultProperty = new DefaultProperty();
        BeanUtils.copyProperties(obj, defaultProperty);
        return defaultProperty;
    }

    /**
     * 查询历史数据
     *
     * @param obj
     * @param t
     * @param pagebase
     * @return
     */
    public static Example defaultDisposeByHistory(Object obj, Class t, pagebase pagebase) {
        DefaultProperty record = copy(obj);
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        defaultDispose(criteria, record, t);
        if (pagebase.getBegintime() != null && pagebase.getEndtime() != null) {
            criteria.andBetween("datatime", pagebase.getBegintime().replace("%20", " "),
                    pagebase.getEndtime().replace("%20", " "));
        }
        //example.setOrderByClause("datatime desc");
        example.orderBy("datatime").desc();
        
        return example;
    }

    /**
     * 查询历史数据通过id
     *
     * @param obj
     * @param t
     * @param pagebase
     * @return
     */
    public static Example defaultDisposeByHistoryId(Object obj, Class t, pagebase pagebase, Integer id) {
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        if (pagebase.getBegintime() != null && pagebase.getEndtime() != null) {
            criteria.andBetween("datatime", pagebase.getBegintime().replace("%20", " "),
                    pagebase.getEndtime().replace("%20", " "));
        }
        example.orderBy("datatime").desc();
        return example;
    }

    /**
     * 查询历史数据,有排除项
     *
     * @param obj
     * @param t
     * @param pagebase
     * @return
     */
    public static Example defaultDisposeByHistory(Object obj, Class t, pagebase pagebase, String[] properties) {
        DefaultProperty record = copy(obj);
        Example example = new Example(t);
        example.selectProperties(properties);
        Example.Criteria criteria = example.createCriteria();
        defaultDispose(criteria, record, t);
        if (pagebase.getBegintime() != null && pagebase.getEndtime() != null) {
            criteria.andBetween("datatime", pagebase.getBegintime().replace("%20", " "),
                    pagebase.getEndtime().replace("%20", " "));
        }

        example.orderBy("datatime").desc();
        return example;
    }

    /**
     * 查询历史数据（无分页）
     *
     * @param obj
     * @param t
     * @param pagebase
     * @return
     */
    public static Example defaultDisposeByHistory(Object obj, Class t) {
        DefaultProperty record = copy(obj);
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        defaultDispose(criteria, record, t);
        example.orderBy("datatime").desc();
        return example;
    }

    public static Example defaultDisposeAddrInfo(Object obj, Class t) {
        DefaultProperty record = copy(obj);
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        defaultDispose(criteria, record, t);
        criteria.andIsNotNull("deviceaddr");
        criteria.andEqualTo("inuse", 1);
        criteria.andCondition(" GROUP BY deviceaddr");
        return example;
    }
}
